
package com.dhcy.mqtt.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {
	/**
	 * 获取当前时间的时间戳
	 */
	public static Integer timeStamp() {
		Integer timeStamp = Integer.parseInt(Calendar.getInstance().getTimeInMillis()/1000+"");
		return timeStamp;
	}
	/**
	 * 将时间戳转换为时间
	 */
	public static String stampToDate(String s){
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long lt = new Long(s);
		Date date = new Date(lt);
		res = simpleDateFormat.format(date);
		return res;
	}
	/**
	 * 获取当前日子
	 */
	public static int getYear() {
		Calendar cal=Calendar.getInstance();
		int d=cal.get(Calendar.YEAR);
		return d;
	}

	/**
	 * 获取当前日子
	 */
	public static int getDay() {
		Calendar cal=Calendar.getInstance();
		int d=cal.get(Calendar.DATE);
		return d;
	}
	/**
	 * 根据当前日期获得上周的日期区间（上周周一和周日日期）
	 * @return
	 */
	 public static String getLastTimeInterval() {
		 DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		 DateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
         Calendar calendar1 = Calendar.getInstance();
         Calendar calendar2 = Calendar.getInstance();
         int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
         int offset1 = 1 - dayOfWeek;
         int offset2 = 7 - dayOfWeek;
         calendar1.add(Calendar.DATE, offset1 - 7);
         calendar2.add(Calendar.DATE, offset2 - 7);
         // System.out.println(sdf.format(calendar1.getTime()));// last Monday
         String lastBeginDate = sdf.format(calendar1.getTime());
         // System.out.println(sdf.format(calendar2.getTime()));// last Sunday
         String lastEndDate = sdf1.format(calendar2.getTime());
         return lastBeginDate + "," + lastEndDate;
    }




	/**
	 * 将时间戳转换为Date
	 * @throws ParseException
	 *
	 */
	public static String stampToDate1(Integer in)  {

		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		long lt = new Long(in+"000");
		Date date = new Date(lt);
		String  res = sdf.format(date);

		return res;

	}



	/**
	 * 获取当天21：30的时间戳
	 * @throws ParseException
	 */
	public static long nineStamp()  {
		long ts=0;
		try {

			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date now = new Date();
			Date date = df.parse(df.format(now));
			String str = df.format(now)+" 21:30:00";
			DateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			Date datee = dff.parse(str);
			ts = datee.getTime() / 1000;
		} catch (Exception e) {
			e.printStackTrace();

		}

		return ts;
	}

	/**
	 * 时间戳转换成日期格式字符串
	 * @param seconds 精确到秒的字符串
	 * @return
	 */
	public static String timeStamp2Date(int seconds) {
		if(seconds==0){
			return  null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date(Long.valueOf(seconds+"000")));
	}
	/**
	 * 时间戳转换成日期格式字符串
	 * @param seconds 精确到天的字符串
	 * @param
	 * @return
	 */
	public static String timeStamp2Date2(int seconds) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date(Long.valueOf(seconds+"000")));
	}



	/**
	 * 获取今年的时间戳（年初，年末）
	 * @throws ParseException
	 */
	public static Map<String, Object> yearStamp(String year)  {
		long ts=0;
		long ts1=0;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			DateFormat df = new SimpleDateFormat("yyyy");
			Date now = new Date();
			Date date = df.parse(df.format(now));
			String str = year+"-01-01 00:00:00";
			DateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String str1 = year+"-12-31 23:59:59";
			DateFormat dff1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			ts = dff.parse(str).getTime() / 1000;
			ts1 = dff.parse(str1).getTime() / 1000;
			map.put("start", ts);
			map.put("end", ts1);
		} catch (Exception e) {
			e.printStackTrace();

		}

		return map;
	}
	/**
	 * 获取今天的时间戳（12----00）
	 * @throws ParseException
	 */
	public static Map<String, Object> DateStamp()  {
		long ts=0;
		long ts1=0;
		Map<String, Object> map = new HashMap<String, Object>();
		try {

			DateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			//Java 获取前一天起始时间和结束时间
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, 0);
			String yesterday = new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime());
			String yesterdayBegin = yesterday + " 00:00:00";
			String yesterdayEnd = yesterday + " 23:59:59";


			ts = dff.parse(yesterdayBegin).getTime() / 1000;
			ts1 = dff.parse(yesterdayEnd).getTime() / 1000;
			map.put("start", ts);
			map.put("end", ts1);
		} catch (Exception e) {
			e.printStackTrace();

		}

		return map;
	}
	/**
	 * 获取今天指定时间的时间戳
	 */
	public static String DateZhiDing(String str) {
		 Date d = new Date();
	     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd "+str);
	     String dateNowStr = sdf.format(d);
	     SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	     try {
			return String.valueOf(sd.parse(dateNowStr).getTime()/1000);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	     return "";

	}



	/**
	 * 获取指定时间的的时间戳（12----00）
	 * @throws ParseException
	 */
	public static Map<String, Object> DateStamp1(String str)  {
		long ts=0;
		long ts1=0;
		Map<String, Object> map = new HashMap<String, Object>();
		try {

			DateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			String yesterdayBegin = str + " 00:00:00";
			String yesterdayEnd = str + " 23:59:59";


			ts = dff.parse(yesterdayBegin).getTime() / 1000;
			ts1 = dff.parse(yesterdayEnd).getTime() / 1000;
			map.put("start", ts);
			map.put("end", ts1);
		} catch (Exception e) {
			e.printStackTrace();

		}

		return map;
	}
	//获取置顶日期的星期
	public static String getWeekday(String date){//必须yyyy-MM-dd
		 SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
	        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
	        Calendar cal = Calendar.getInstance(); // 获得一个日历
	        Date datet = null;
	        try {
	            datet = f.parse(date);
	            cal.setTime(datet);
	        } catch (ParseException e) {
	            e.printStackTrace();
	        }
	        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
	        if (w < 0)
	            w = 0;
	        return weekDays[w];
	}

	//返回英文星期
	public static String getWeekByMap(String week) {
		Map<String,String> map=new HashMap<String,String>();
		map.put("星期一", "Monday");
		map.put("星期二", "Tuesday");
		map.put("星期三", "Wednesday");
		map.put("星期四", "Thursday");
		map.put("星期五", "Friday");
		map.put("星期六", "Saturday");
		map.put("星期日", "Sunday");
		return map.get(week);
	}
	//月份大小
	public static String getMonSize(String mon) {
		String[] str = {"01","03","05","07","08","10","12"};
		String[] str1 = {"02","04","06","09","11"};
		if(Arrays.asList(str).contains(mon)) {
			return mon+"月大";
		}
		return mon+"月小";
	}

	/*
     * 将时间转换为时间戳
     */
    public static long dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return Long.parseLong(res)/1000;
    }
	/**
	 * 判断几天前
	 * @param
	 * @return
	 */
  	public static String getTimeAfter(int time) {
  		if(time>31536000) {
  			return time/31536000+"年前";
  		}else if(time>86400) {
  			return time/86400+"天前";
		}else if(time>3600) {
			return time/3600+"小时前";
		}else {
			return time/60+"分钟前";
		}
  	}
/**
      * 日期格式字符串转换成时间戳
      *
      * @param dateStr
      *            字符串日期
      * @param format
      *            如：yyyy-MM-dd HH:mm:ss
      *
      * @return
      */
		public static String Date2TimeStamp(String dateStr) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				return String.valueOf(sdf.parse(dateStr).getTime() / 1000);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "";
		}

	/**
	 * 判断几天前
	 *
	 * @param
	 * @return
	 */
	public static String getAfterTime(int time1) {
		int time = timeStamp() - time1;
		int year = Integer.parseInt(timeStamp2Date(time1).substring(0, 4));
		if (getYear() != year) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String format = simpleDateFormat.format((long)time1*1000);
			return format;
		} else if (time > 86400) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
			String format = simpleDateFormat.format((long)time1*1000);
			return format;
		} else if (time > 21600) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
			String format = simpleDateFormat.format((long)time1*1000);
			return format;
		} else if (time > 3600) {
			return time / 3600 + "小时前";
		} else {
			return time / 60 + "分钟前";
		}
	}

	/**
	 * 时间格式
	 * 上午下午   周几  年月日
	 *
	 * @param
	 * @return
	 */
	public static String timeFormat(int time1) throws ParseException {
		Map<String, Object> map = DateStamp();
		int start = Integer.parseInt(map.get("start").toString());
		int end = Integer.parseInt(map.get("end").toString());
//		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("a hh:mm",Locale.CHINA);
		if (time1 > start && time1 < end) {
			/*String format = simpleDateFormat1.format((long) time1 * 1000);
			Date date=simpleDateFormat1.parse(format);
			GregorianCalendar ca = new GregorianCalendar();
			ca.setTime(date);
			String ap = ca.get(GregorianCalendar.AM_PM) == Calendar.PM ? "下午" : "上午";*/
			String format1 = simpleDateFormat3.format((long) time1 * 1000);
			return /*ap+*/format1;
		} else if (time1 > start-86400 && time1 < end-86400) {
			return "昨天";
		} else if (time1 > start-604800 && time1 < end) {
			String format = simpleDateFormat2.format((long) time1 * 1000);
			String weekday = getWeekday(format);
			return weekday;
		}else {
			String format = simpleDateFormat2.format((long) time1 * 1000);
			return format;
		}
	}

	//计算当前时间距首次开奖时间的秒
	public static int getBetMillis(String time){
		Calendar calendar=Calendar.getInstance();
		String[] times=time.split(":");
		calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(times[0]));
		calendar.set(Calendar.MINUTE,Integer.parseInt(times[1]));
		calendar.set(Calendar.SECOND,Integer.parseInt(times[2]));
		long betMillis = System.currentTimeMillis() - calendar.getTimeInMillis();
		return  (int) (betMillis/1000);
	}


	public static final String parseDateToStr( final Date date)
	{
		String format = "yyyy-MM-dd HH:mm:ss";
		return new SimpleDateFormat(format).format(date);
	}

}
